package com.itheima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.exception.SystemException;
import com.itheima.mapper.EmployeeMapper;
import com.itheima.pojo.Code;
import com.itheima.pojo.Employee;
import com.itheima.pojo.Result;
import com.itheima.service.EmployeeService;
import com.itheima.util.JwtUtils;
import com.itheima.vo.EmployeeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import org.springframework.web.bind.annotation.RequestBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {


    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private RedisTemplate redisTemplate;




    // 登录
    @Override
    public Result login(Employee employee) {
        // 查询是否表里有该数据
        QueryWrapper qw = new QueryWrapper();
        qw.eq("username", employee.getUsername());
        qw.eq("password", employee.getPassword());
        Employee one = employeeMapper.selectOne((Wrapper<Employee>) qw);
        if (one == null) {
            throw new SystemException(Code.ERR404, null);
        }
        EmployeeVo vo = new EmployeeVo();
        // list数据转发给vo
        BeanUtil.copyProperties(one, vo);
        //生成token
        HashMap<String, String> map = new HashMap<>();
        map.put("emp", JSON.toJSONString(vo));
        String token = JwtUtils.getToken(map);
        vo.setToken(token);
        redisTemplate.opsForValue().set(token, JSON.toJSONString(vo), 1, TimeUnit.DAYS);
        return new Result(Code.OK, vo, "{}");

    }


    //分页查询
    @Override
    public Result page(String name, Integer page, Integer pageSize) {
        QueryWrapper wrapper = new QueryWrapper();
        if (!StrUtil.isBlank(name)) {
            wrapper.eq("name", name);
        }
        Page page1 = employeeMapper.selectPage(new Page<>(page, pageSize), wrapper);
        return new Result(Code.OK, page1,"{}");
    }


    //根据id查询员工
    @Override
    public Result getid(String id) {
        Employee employee = employeeMapper.selectById(id);

        return new Result(Code.OK, employee,"{}");
    }


    //添加员工
    @Override
    public Result add(HttpServletRequest request, @RequestBody Employee employee) {
        QueryWrapper<Employee> wrapper = new QueryWrapper<>();
        wrapper.eq("username", employee.getUsername());
        Employee one = employeeMapper.selectOne(wrapper);
        if (one == null) {
            // //验证令牌
            // String token = request.getHeader("Authorization");
            // Object o = redisTemplate.opsForValue().get(token);
            // Employee emp = JSON.parseObject(o.toString(), Employee.class);
            // String name = emp.getName();
            // employee.setCreateUser(name);
            // employee.setCreateTime(System.currentTimeMillis());
            int num = employeeMapper.insert(employee);
            if (num > 0) {
                return new Result(Code.OK, "添加成功", null);
            }
            return new Result(Code.ERR401, "未添加成功","{}");
        }
        return new Result(Code.ERR401, "该用户名已存在，请重新输入","{}");

    }


    /**
     * 修改员工
     */
    @Override
    public Result put(Employee employee) {

        // //通过token获取当前用户作为修改人
        // String token = request.getHeader("Authorization");
        // Object o = redisTemplate.opsForValue().get(token);
        // Employee employee1 = JSON.parseObject(o.toString(), Employee.class);
        // String name = employee1.getName();
        //
        // employee.setUpdateUser(name);
        // employee.setUpdateTime(System.currentTimeMillis());

        int num = employeeMapper.updateById(employee);
        if (num > 0) {
            return new Result(Code.OK, "修改员工信息成功", "{}");
        }
            return new Result(Code.ERR401, "修改失败","{}");

    }
}

